<template>
  <div class="dashboard-container">
    <!-- 顶部信息栏 -->
    <div class="header-info">
      <h1>数据可视化大屏</h1>
      <div class="user-info">
        <span>欢迎回来，{{ userInfo?.username }}</span>
        <span class="date">{{ currentDate }}</span>
      </div>
    </div>

    <!-- 数据概览卡片 -->
    <div class="stats-container">
      <div class="stat-card">
        <div class="stat-title">总用户数</div>
        <div class="stat-value">{{ userCount }}</div>
        <div class="stat-change">
          <span class="up">+12%</span>
          <span class="compared">较上月</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-title">今日新增</div>
        <div class="stat-value">{{ todayNewUsers }}</div>
        <div class="stat-change">
          <span class="up">+8</span>
          <span class="compared">较昨日</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-title">活跃用户</div>
        <div class="stat-value">{{ activeUsers }}</div>
        <div class="stat-change">
          <span class="up">+5%</span>
          <span class="compared">较上周</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-title">平均在线时长</div>
        <div class="stat-value">{{ avgOnlineTime }}分钟</div>
        <div class="stat-change">
          <span class="down">-2%</span>
          <span class="compared">较上周</span>
        </div>
      </div>
    </div>

    <!-- 图表区域 -->
    <div class="charts-container">
      <div class="chart-item">
        <div class="chart-title">用户增长趋势</div>
        <div id="userGrowthChart" class="chart"></div>
      </div>
      <div class="chart-item">
        <div class="chart-title">用户地区分布</div>
        <div id="userDistributionChart" class="chart"></div>
      </div>
      <div class="chart-item">
        <div class="chart-title">用户活跃度</div>
        <div id="userActivityChart" class="chart"></div>
      </div>
      <div class="chart-item">
        <div class="chart-title">流量来源分析</div>
        <div id="trafficSourceChart" class="chart"></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { getUserList } from '@/api/user'
// 修改 ECharts 导入方式
import { ref as echartsRef } from 'vue'
import * as echarts from 'echarts'

// 响应式数据
const userInfo = ref<any>(null)
const currentDate = ref('')
const userCount = ref(0)
const todayNewUsers = ref(0)
const activeUsers = ref(0)
const avgOnlineTime = ref(0)

// 图表数据
const userGrowthData = ref<number[]>([])
const userDistributionData = ref<any[]>([])
const userActivityData = ref<number[]>([])
const trafficSourceData = ref<any[]>([])

// 初始化图表
const initCharts = () => {
  // 用户增长趋势图表
  const userGrowthChart = echarts.init(document.getElementById('userGrowthChart'))
  userGrowthChart.setOption(userGrowthOption.value)

  // 用户地区分布图表
  const userDistributionChart = echarts.init(document.getElementById('userDistributionChart'))
  userDistributionChart.setOption(userDistributionOption.value)

  // 用户活跃度图表
  const userActivityChart = echarts.init(document.getElementById('userActivityChart'))
  userActivityChart.setOption(userActivityOption.value)

  // 流量来源分析图表
  const trafficSourceChart = echarts.init(document.getElementById('trafficSourceChart'))
  trafficSourceChart.setOption(trafficSourceOption.value)

  // 响应窗口大小变化
  window.addEventListener('resize', () => {
    userGrowthChart.resize()
    userDistributionChart.resize()
    userActivityChart.resize()
    trafficSourceChart.resize()
  })
}

// 初始化数据
onMounted(() => {
  // 获取用户信息
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    userInfo.value = JSON.parse(userInfoStr)
  }

  // 设置当前日期
  const now = new Date()
  currentDate.value = now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })

  // 模拟数据
  fetchDashboardData()
})

// 获取仪表盘数据
const fetchDashboardData = async () => {
  try {
    // 实际项目中，这里会调用API获取真实数据
    // 这里使用模拟数据
    userCount.value = 12580
    todayNewUsers.value = 156
    activeUsers.value = 8642
    avgOnlineTime.value = 42

    // 模拟趋势数据
    userGrowthData.value = [6500, 7200, 8100, 9300, 10500, 11800, 12580]

    // 模拟地区分布数据
    userDistributionData.value = [
      { name: '华东', value: 3500 },
      { name: '华北', value: 2800 },
      { name: '华南', value: 2200 },
      { name: '西南', value: 1800 },
      { name: '西北', value: 1200 },
      { name: '东北', value: 1000 },
      { name: '其他', value: 80 }
    ]

    // 模拟活跃度数据
    userActivityData.value = [2500, 1800, 3200, 2100, 2800, 4200, 3800]

    // 模拟流量来源数据
    trafficSourceData.value = [
      { name: '直接访问', value: 35 },
      { name: '搜索引擎', value: 45 },
      { name: '社交媒体', value: 15 },
      { name: '广告推广', value: 5 }
    ]
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
  }
}

// 用户增长趋势图表配置
const userGrowthOption = computed(() => ({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '用户数',
      type: 'line',
      smooth: true,
      data: userGrowthData.value,
      lineStyle: {
        color: '#36CFC9'
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(54, 207, 201, 0.5)' },
          { offset: 1, color: 'rgba(54, 207, 201, 0.1)' }
        ])
      },
      markPoint: {
        data: [
          { type: 'max', name: '最大值' },
          { type: 'min', name: '最小值' }
        ]
      }
    }
  ]
}))

// 用户地区分布图表配置
const userDistributionOption = computed(() => ({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '用户分布',
      type: 'pie',
      radius: '50%',
      data: userDistributionData.value,
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      },
      label: {
        formatter: '{b}: {c} ({d}%)'
      }
    }
  ]
}))

// 用户活跃度图表配置
const userActivityOption = computed(() => ({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '活跃用户',
      type: 'bar',
      data: userActivityData.value,
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#597EF7' },
          { offset: 1, color: '#83A5FF' }
        ])
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值' }
        ]
      }
    }
  ]
}))

// 流量来源分析图表配置
const trafficSourceOption = computed(() => ({
  tooltip: {
    trigger: 'item'
  },
  series: [
    {
      name: '流量来源',
      type: 'funnel',
      left: '10%',
      width: '80%',
      data: trafficSourceData.value,
      emphasis: {
        label: {
          fontSize: 20
        }
      },
      labelLine: {
        length: 10
      },
      label: {
        formatter: '{b}: {c}%'
      }
    }
  ]
}))
</script>

<style lang="scss" scoped>
.dashboard-container {
  width: 100%;
  height: 100%;
  padding: 20px;
  background-color: #f0f2f5;
  box-sizing: border-box;
}

.header-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.date {
  color: #666;
  font-size: 14px;
  margin-top: 4px;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
}

.stat-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.stat-change {
  display: flex;
  align-items: center;
  font-size: 12px;
}

.up {
  color: #00B42A;
  margin-right: 4px;
}

.down {
  color: #F53F3F;
  margin-right: 4px;
}

.compared {
  color: #999;
}

.charts-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.chart-item {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
}

.chart-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 16px;
  font-weight: bold;
}

.chart {
  width: 100%;
  height: 300px;
}
</style>